import {
  mSelectDevTypeByPage,
  selectDevBaseDataList,
  selectDevDataTypeList,
  selectDevTxTypeList
} from '@/api/device'
import {selectItemByTypeSign} from '@/api/information'
import type {ActionContext} from 'vuex'
import {RootState} from '..'

export type DeviceState = {
  // 设备类型列表
  devTypeList: DeviceType.SimpleType[]
  devTypeListLoading: boolean

  // 监测参数列表
  checkParamList: SysDict.Item[]
  checkParamListLoading: boolean

  // 设备通信类型列表 mqtt socket
  devTxTypeList: SysDict.Item[]
  devTxTypeListLoading: boolean

  // 设备通信协议类型 json modbus
  devDataTypeList: SysDict.Item[]
  devDataTypeListLoading: boolean

  // 设备基础信息列表
  devBaseDataList: SysDict.Item[]
  devBaseDataListLoading: boolean
}
export type DeviceContext = ActionContext<DeviceState, RootState>

export const state: DeviceState = {
  checkParamList: [],
  checkParamListLoading: false,

  devTypeList: [],
  devTypeListLoading: false,

  devTxTypeList: [],
  devTxTypeListLoading: false,

  devDataTypeList: [],
  devDataTypeListLoading: false,

  devBaseDataList: [],
  devBaseDataListLoading: false
}

export const getters = {
  checkParamList(state: DeviceState) {
    return state.checkParamList
  },
  devTypeList(state: DeviceState) {
    return state.devTypeList
  },
  devTxTypeList(state: DeviceState) {
    return state.devTxTypeList
  },
  devDataTypeList(state: DeviceState) {
    return state.devDataTypeList
  },
  devBaseDataList(state: DeviceState) {
    return state.devBaseDataList
  }
}

export const mutations = {
  setCheckParamList(state: DeviceState, data: SysDict.Item[]) {
    state.checkParamList = data
  },
  clearCheckParamList(state: DeviceState) {
    state.checkParamList = []
  },
  startCheckParamListLoading(state: DeviceState) {
    state.checkParamListLoading = true
  },
  closeCheckParamListLoading(state: DeviceState) {
    state.checkParamListLoading = false
  },
  //!  -----------------------------------
  setDevTypeList(state: DeviceState, data: DeviceType.SimpleType[]) {
    state.devTypeList = data
  },
  addDevTypeList(state: DeviceState, data: DeviceType.SimpleType[]) {
    state.devTypeList.push(...data)
  },
  clearDevTypeList(state: DeviceState, data: DeviceType.SimpleType[]) {
    state.devTypeList = []
  },
  startDevTypeListLoading(state: DeviceState) {
    state.devTypeListLoading = true
  },
  closeDevTypeListLoading(state: DeviceState) {
    state.devTypeListLoading = false
  },
  //!  -----------------------------------
  setDevTxTypeList(state: DeviceState, data: SysDict.Item[]) {
    state.devTxTypeList = data
  },
  clearDevTxTypeList(state: DeviceState, data: DeviceType.SimpleType[]) {
    state.devTxTypeList = []
  },
  startDevTxTypeListLoading(state: DeviceState) {
    state.devTxTypeListLoading = true
  },
  closeDevTxTypeListLoading(state: DeviceState) {
    state.devTxTypeListLoading = false
  },
  //!  -----------------------------------
  setDevDataTypeList(state: DeviceState, data: SysDict.Item[]) {
    state.devDataTypeList = data
  },
  clearDevDataTypeList(state: DeviceState, data: DeviceType.SimpleType[]) {
    state.devDataTypeList = []
  },
  startDevDataTypeListLoading(state: DeviceState) {
    state.devDataTypeListLoading = true
  },
  closeDevDataTypeListLoading(state: DeviceState) {
    state.devDataTypeListLoading = false
  },
  //!  -----------------------------------
  setDevBaseDataList(state: DeviceState, data: SysDict.Item[]) {
    state.devBaseDataList = data
  },
  clearDevBaseDataList(state: DeviceState, data: DeviceType.SimpleType[]) {
    state.devBaseDataList = []
  },
  startDevBaseDataListLoading(state: DeviceState) {
    state.devBaseDataListLoading = true
  },
  closeDevBaseDataListLoading(state: DeviceState) {
    state.devBaseDataListLoading = false
  }
}

export const actions = {
  /**获取所有检测参数 */
  updateAllCheckParam: ({commit, dispatch, state}: DeviceContext) => {
    if (state.checkParamListLoading) return
    commit('startCheckParamListLoading')
    selectItemByTypeSign({dataType: 'checkParam'})
      .then(res => {
        commit('closeCheckParamListLoading')

        commit('setCheckParamList', res.data)
      })
      .catch(err => {
        commit('closeCheckParamListLoading')

        commit('clearCheckParamList')
      })
  },

  /**获取所有设备类型 */
  updateSelectDevTypeList: (
    {commit, dispatch}: DeviceContext,
    option?: {
      devTypeName?: string
      page?: number
      size?: number
      success?: (data: PagingResponse<DeviceType.SimpleType>) => void
      error?: Function
    }
  ) => {
    if (state.devTypeListLoading) return

    commit('startDevTypeListLoading')
    mSelectDevTypeByPage({
      page: option ? (option.page !== undefined ? option.page : 1) : 1,
      size: option ? (option.size !== undefined ? option.size : 9999) : 99999,
      devTypeName: option?.devTypeName
    })
      .then(res => {
        commit('closeDevTypeListLoading')
        commit('addDevTypeList', res.data.list)
        option?.success?.(res.data)
      })
      .catch(err => {
        commit('closeDevTypeListLoading')
        commit('clearDevTypeList')
        option?.error?.()
      })
  },

  /**获取通讯类型列表 */
  updateDevTxTypeList: (
    {commit, dispatch}: DeviceContext,
    option?: {
      success?: (data: SysDict.Item[]) => void
      error?: Function
    }
  ) => {
    if (state.devTxTypeListLoading) return

    commit('startDevTxTypeListLoading')

    selectDevTxTypeList()
      .then(res => {
        commit('closeDevTxTypeListLoading')

        commit('setDevTxTypeList', res.data)
        option?.success?.(res.data)
      })
      .catch(err => {
        commit('closeDevTxTypeListLoading')

        commit('clearDevTxTypeList')
        option?.error?.()
      })
  },

  /**获取通讯协议类型列表 */
  updateDevDataTypeList: (
    {commit, dispatch}: DeviceContext,
    option?: {
      success?: (data: SysDict.Item[]) => void
      error?: Function
    }
  ) => {
    if (state.devDataTypeListLoading) return

    commit('startDevDataTypeListLoading')

    selectDevDataTypeList()
      .then(res => {
        commit('closeDevDataTypeListLoading')

        commit('setDevDataTypeList', res.data)
        option?.success?.(res.data)
      })
      .catch(err => {
        commit('closeDevDataTypeListLoading')

        commit('clearDevDataTypeList')
        option?.error?.()
      })
  },
  /**获取基本信息列表 */
  updateDevBaseDataList: (
    {commit, dispatch}: DeviceContext,
    option?: {
      success?: (data: SysDict.Item[]) => void
      error?: Function
    }
  ) => {
    if (state.devDataTypeListLoading) return

    commit('startDevBaseDataListLoading')

    selectDevBaseDataList()
      .then(res => {
        commit('closeDevBaseDataListLoading')

        commit('setDevBaseDataList', res.data)
        option?.success?.(res.data)
      })
      .catch(err => {
        commit('closeDevBaseDataListLoading')

        commit('clearDevBaseDataList')
        option?.error?.()
      })
  }
}

export default {
  namespaced: true, // 声明命名空间
  state,
  mutations,
  actions,
  getters
}
